/*
Filename: UCAdminLogin.java
Precondition: Get the password and administrator from administrator login view.
Post-condition: The administrator can login and can add or delete managers.
Main success Scenario:
1. Administrator selects to login
2. System retrieves administrator name and password
3. Administrator clicks ‘Login’ button
4. System verifies the login information
5. System logs the administrator in
6. System updates the main UI
2a. Extensions: System detects invalid login.
System display “invalid administrator login” message

 */

package ucm;

/**
 *
 * @author Mark
 */
import model.UsersModel;
import model.DatabaseModel;
import view.AdminLogin;

import javax.swing.*;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;

public class UCAdminLogin  implements UCController {

    private UsersModel user;

    public UCAdminLogin() {
        user = new UsersModel("", "", "", "", "");
    }

    public UCAdminLogin(UsersModel user) {
        // System retrieves username and password
        this.user = user;
    }

    public UCAdminLogin(AdminLogin AdminLogin) {
        // System retrieves username and password
        this.user = AdminLogin.getUserModel();
    }

    // STEP 1 - User Selects Login
    public boolean run() {
        boolean loggedIn = false;
        // STEP 2 - System retrieves username and password
        // This step is integrated into the constructor or
        // may be specified by using run(UserModel)
        // STEP 3 - System validates the username and password
        if (validUser()) {
            // STEP 4 - System displays main view
            JOptionPane.showMessageDialog(new JFrame(),
                    "Login Successful!",
                    "Success",
                    JOptionPane.INFORMATION_MESSAGE);
            loggedIn = true;
            new UCDisplayMgrInformation();

        } else {
            // STEP 2a - System display “invalid user login” message
            JOptionPane.showMessageDialog(new JFrame(),
                    "Invalid Username or Password. Try again.",
                    "Error Message",
                    JOptionPane.ERROR_MESSAGE);
            new ucm.UCDisplayAdminLogin();
        }
        return loggedIn;
    }

    private boolean validUser() {
        boolean correct = false;
        String username = user.getUsername();
        String password = user.getPassword();
        DatabaseModel dbModel = DatabaseModel.getInstance();

        // Check whether we should be using ldap authentication or
        // just regular
        if (dbModel.ldapEnabled()) {
            if (username.equals("") || password.equals("")) {
                correct = false;
               }

            // Else we are just using standard database lookup
        } else {
            Connection con = dbModel.getConnection();
            // Make sure there is a connection
            if (con == null) {
                JOptionPane.showMessageDialog(new JFrame(),
                        "Connection to database failed",
                        "Error Message",
                        JOptionPane.ERROR_MESSAGE);
            } else {
                try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE,
                    ResultSet.CONCUR_UPDATABLE);

            ResultSet res = stmt.executeQuery("SELECT * FROM admins WHERE " +
                    "aname = '" + username + "'");

            correct = res.first() && username.equals(res.getString("aname"))
                    && password.equals(res.getString("apassword"));

               } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return correct;
    }
}
